#!/bin/bash

usage()
{
    echo "usage:"
    echo "cxclass [classname]"
}


if [ $# -ne 1 ]; then
    usage
    exit 1
fi

CLASSNAME=$1

echo ${CLASSNAME} |grep -e "^[a-zA-Z]" > /dev/null
if [ $? -ne 0 ]; then
    echo "Class name must start with a letter.. doofus"
    exit 1
fi

HFILE=${CLASSNAME}.h
CPPFILE=${CLASSNAME}.cpp

if [ -f ${HFILE} ] || [ -f ${CPPFILE} ]; then
    echo "ERROR: Already exist: ${HFILE} and/or ${CPPFILE}"
    exit 1
fi


start_HFILE()
{
    echo "${1}" > ${HFILE}
}
append_HFILE()
{
    echo "${1}" >> ${HFILE}
}
start_CPPFILE()
{
    echo "${1}" > ${CPPFILE}
}
append_CPPFILE()
{
    echo "${1}" >> ${CPPFILE}
}
echo "Generating: ${CLASSNAME}.cpp and ${CLASSNAME}.h"
WHOAMI=`whoami`
DATE=`date`
start_HFILE "/*Generated by ${WHOAMI} at ${DATE} on ${HOSTNAME}"
append_HFILE "*/"
append_HFILE "#ifndef ${CLASSNAME}_H"
append_HFILE "#define ${CLASSNAME}_H"
append_HFILE "#include <iostream>"
append_HFILE ""
append_HFILE "class ${CLASSNAME}{"
append_HFILE " public:"
append_HFILE " ${CLASSNAME}();"
append_HFILE " std::ostream &operator<<(std::ostream &);"
append_HFILE "};"
append_HFILE "#endif"


start_CPPFILE "/*Generated by ${WHOAMI} at ${DATE} on ${HOSTNAME}"
append_CPPFILE "*/"
append_CPPFILE "#include \"${CLASSNAME}.h\""
append_CPPFILE ""
append_CPPFILE "${CLASSNAME}::${CLASSNAME}()"
append_CPPFILE "{"
append_CPPFILE "  std::cout << \"hello from ${CLASSNAME} constructor\" << std::endl;"
append_CPPFILE "}"

append_CPPFILE "std::ostream& ${CLASSNAME}::operator<<(std::ostream &out){"
append_CPPFILE "  out << \"hello from ${CLASSNAME} operator\" << std::endl;"
append_CPPFILE "  return out;"
append_CPPFILE "}"

